//
// Created by jmdvirus on 2019/11/18 下午4:06.
// Copyright (c) 2019 jmdvirus All rights reserved.
//
//     ******          *             *--*
//       ||            *             |
//       ||  *--*--*---*  * *-********--*
//   *  //   *  *  *   *\ / ||  |  |    |
//    **    *  *  *---* *  *   *--* *--*
//

#include "CMShowQtParabola.h"

CMShowQtParabola::CMShowQtParabola(QWidget *widget) { setParent(widget); }

int CMShowQtParabola::showParabola(int x, int y, int r)
{
    if (r > y)
        return -1;
    int y1 = y - r;

    int w = 800;
    int h = 600;
    printf("w = %d, h = %d\n", w, h);
    printf("x = %d, y = %d, r = %d\n", x, y, r);

    for (int i = 0; i < x; i++)
    {
        int r1 = (w / 2 - i) * (w / 2 - i) / (h - 2 * y1) + (h - 2 * y1) / 4;
        if (r1 + y1 <= h / 2)
        {
            m_points[i]     = r1 + y1;
            m_points[w - i] = r1 + y1;
        }
    }

    int c = 1;
    for (auto i : m_points)
    {
        printf("[%d, %d]", i.first, i.second);
        if (c++ % 10 == 0)
        {
            printf("\n");
        }
    }
    printf("\n");
    repaint();
}

void CMShowQtParabola::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen     pen(Qt::green, 5, Qt::DotLine, Qt::RoundCap, Qt::RoundJoin);
    painter.setPen(pen);
    for (auto i : m_points)
    {
        painter.drawPoint(i.first, i.second);
    }
}
